package reports

import (
	"bytes"
	"fmt"
	"strconv"
	"strings"
	"time"

	"shqsoft.cn/pas3ui/base"
)

func GetChartCardData(reptName string, date time.Time, cyno, timeLevel, emplLevel string, emplID int, colName string) (lineDatas []map[string]string, chainData map[string]string, err error) {
	var conf *Config
	conf, err = GetReportConfig(reptName)
	if err != nil {
		return
	}
	paramVals := make(map[string]interface{}, 6)
	paramVals["date"] = date
	paramVals["cyno"] = cyno
	paramVals["timeLevel"] = timeLevel
	paramVals["emplLevel"] = emplLevel
	paramVals["emplID"] = emplID
	paramVals["colName"] = colName
	paramVals["isChartCard"] = true
	unit := 0
	if strings.HasPrefix(colName, "NUM_") {
		unit = 1
	}
	var pName string
	tName := conf.DataSource.TmpTableName
	for k, v := range paramVals {
		pName = "@" + k
		var val string
		if dVal, ok := v.(time.Time); ok {
			val = dVal.Format("20060102")
		} else {
			val = base.ToString(v)
		}
		tName = strings.Replace(tName, pName, val, -1)
	}
	if strings.Contains(tName, "@") {
		err = fmt.Errorf("解析报表%s的表名出错，解析之后是%s！", conf.Name, tName)
		return
	}
	err = createLineColumnData(conf, tName, paramVals)
	if err != nil {
		return
	}
	lineDatas, _, err = readFromRedis(conf, tName, unit)
	if err != nil {
		return
	}
	hkey := fmt.Sprintf("chain:%s", tName)
	chainData, err = base.RedisCli.HGetAll(ctx, hkey).Result()
	if unit == 0 {
		unit = conf.Unit
	}
	if unit == 0 {
		unit = 1
	}
	for k, v := range chainData {
		val, _ := strconv.ParseFloat(v, 64)
		chainData[k] = fmt.Sprintf("%.2f", val/float64(unit))
	}
	return
}

func getTimeRange(timeLevel string, date time.Time) (mRanges []monthRange, dRanges *dateRange, pdates []string, err error) {
	var sdate time.Time
	var stepSize int
	mRanges = make([]monthRange, 0, 12)
	pdates = make([]string, 0, 13)
	switch timeLevel {
	case "year":
		sdate = time.Date(date.Year()-12, 1, 1, 0, 0, 0, 0, time.Local)
		stepSize = 12
	case "quarter":
		m := 12*3 + date.Month()%3 - 1
		sdate = time.Date(date.Year(), -m, 1, 0, 0, 0, 0, time.Local)
		stepSize = 3
	case "month":
		sdate = time.Date(date.Year(), date.Month()-12, 1, 0, 0, 0, 0, time.Local)
		stepSize = 1
	case "day":
		sdate = date.AddDate(0, 0, -12)
		dRanges = &dateRange{sdate: sdate.Format("2006-01-02"), edate: date.Format("2006-01-02")}
		for sdate.Unix() <= date.Unix() {
			pdates = append(pdates, sdate.Format("2006-01-02"))
			sdate = sdate.AddDate(0, 0, 1)
		}
		return
	default:
		err = fmt.Errorf("参数timeLevel的值%s无效，必须为year,quarter,month或者day", timeLevel)
		return
	}
	var sysDate *time.Time
	sysDate, _, err = base.GetSYSDate("ODS")
	if err != nil {
		return
	}
	var mr monthRange
	var endDate time.Time
	for sdate.Unix() < date.Unix() {
		endDate = sdate.AddDate(0, stepSize, -1)
		if endDate.Unix() > date.Unix() {
			endDate = date
		}
		if endDate.Year() == date.Year() && endDate.Month() == date.Month() && date.Unix() != sysDate.Unix() {
			days := int(date.Sub(sdate).Hours())/24 + 1
			dRanges = &dateRange{sdate: sdate.Format("2006-01-02"), edate: date.Format("2006-01-02"), days: days}
			pdates = append(pdates, date.Format("2006-01-02"))
		} else {
			days := int(endDate.Sub(sdate).Hours())/24 + 1
			mr = monthRange{smon: sdate.Format("200601"), emon: endDate.Format("200601"), days: days}
			mRanges = append(mRanges, mr)
			pdates = append(pdates, endDate.Format("2006-01-02"))
		}
		sdate = sdate.AddDate(0, stepSize, 0)
	}
	return
}
func parseLineColumnParams(repConf *Config, paramVals map[string]interface{}) (colName, where string, err error) {
	if repConf.DataSource.DFTable == "" {
		err = fmt.Errorf("生成报表%s的LineColumnData出错，DataSource.DFTable不能为空", repConf.Name)
		return
	}
	if repConf.DataSource.TmpTableName == "" {
		err = fmt.Errorf("生成报表%s的LineColumnData出错，DataSource.TmpTableName不能为空", repConf.Name)
		return
	}
	var cyno string
	if paramVals["cyno"] != nil {
		cyno = paramVals["cyno"].(string)
	}
	var whereCyno string
	if cyno == "1" {
		whereCyno = ""
	} else if cyno == "2" {
		whereCyno = "CYNO='01'"
	} else if cyno == "3" {
		whereCyno = "CYNO<>'01'"
	} else {
		err = fmt.Errorf("生成报表%s数据出错，参数cyno的值%s无效，必须为1，2或者3", repConf.Name, cyno)
		return
	}
	var emplLevel string
	if paramVals["emplLevel"] != nil {
		emplLevel = paramVals["emplLevel"].(string)
	}
	if emplLevel == "" {
		err = fmt.Errorf("生成报表%s的LineColumnData出错，参数emplLevel不能为空", repConf.Name)
		return
	}
	if paramVals["colName"] != nil {
		colName = paramVals["colName"].(string)
	}
	if colName == "" {
		err = fmt.Errorf("生成报表%s的LineColumnData出错，参数colName不能为空", repConf.Name)
		return
	}
	var emplID, cuid int
	if paramVals["emplID"] != nil {
		emplID = paramVals["emplID"].(int)
	}
	if paramVals["cuid"] != nil {
		cuid = paramVals["cuid"].(int)
	}
	if emplLevel == "all" {
		where = "1=1"
	} else if emplLevel == "dept1" {
		where = fmt.Sprintf("EMID IN (SELECT toUInt32(EMID) FROM DICT_USER_DEPT WHERE PDID=%d)", emplID)
	} else if emplLevel == "dept2" {
		where = fmt.Sprintf("EMID IN (SELECT toUInt32(EMID) FROM DICT_USER_DEPT WHERE DPID=%d)", emplID)
	} else {
		where = fmt.Sprintf("EMID=%d", emplID)
	}
	if cuid > 0 {
		where = fmt.Sprintf("%s and CUID=%d", where, cuid)
	}
	if whereCyno != "" {
		where = fmt.Sprintf("%s and %s", where, whereCyno)
	}
	return
}

//createLineColumnData 生成LineColumn分析数据
func createLineColumnData(repConf *Config, tName string, paramVals map[string]interface{}) (err error) {
	reptLock.Lock(tName)
	defer reptLock.Unlock(tName)
	hkey1 := fmt.Sprintf("clbh:%s", tName)
	hkey2 := fmt.Sprintf("clah:%s", tName)
	count1 := base.RedisCli.Exists(ctx, hkey1).Val()
	count2 := base.RedisCli.Exists(ctx, hkey2).Val()
	if count1 > 0 && count2 > 0 {
		return
	}
	if repConf.DataSource.CacheType != "redis" {
		err = fmt.Errorf("生成报表%s的LineColumnData出错，DataSource.CacheType必须为redis", repConf.Name)
		return
	}
	var timeLevel string
	if paramVals["timeLevel"] != nil {
		timeLevel = paramVals["timeLevel"].(string)
	}
	if timeLevel == "" {
		err = fmt.Errorf("生成报表%s的LineColumnData出错，参数timeLevel不能为空", repConf.Name)
		return
	}
	var where string
	var date time.Time
	var colName string
	if paramVals["date"] != nil {
		date = paramVals["date"].(time.Time)
	}
	if date.IsZero() {
		err = fmt.Errorf("生成报表%s的LineColumnData出错，参数date不能为空", repConf.Name)
		return
	}
	colName, where, err = parseLineColumnParams(repConf, paramVals)
	mRanges, dRanges, pdates, err := getTimeRange(timeLevel, date)
	if err != nil {
		return err
	}
	clen := len(repConf.Columns)
	if clen < 2 || clen > 3 {
		err = fmt.Errorf("生成报表%s的LineColumnData出错，配置错误：这类报表只配置2列或3列", repConf.Name)
		return
	}
	var hasAcbl, hasAvg, hasSum bool
	for _, col := range repConf.Columns {
		if col.GroupCalc == "acbl" {
			if hasAcbl {
				err = fmt.Errorf("生成报表%s的LineColumnData出错，配置错误：groupCalc为acbl的列只能有一个", repConf.Name)
				return
			}
			hasAcbl = true
		} else if col.GroupCalc == "avg" {
			if hasAvg {
				err = fmt.Errorf("生成报表%s的LineColumnData出错，配置错误：groupCalc为avg的列只能有一个", repConf.Name)
				return
			}
			hasAvg = true
		} else if col.GroupCalc == "sum" {
			if hasSum {
				err = fmt.Errorf("生成报表%s的LineColumnData出错，配置错误：groupCalc为sum的列只能有一个", repConf.Name)
				return
			}
			hasSum = true
		}
	}
	if !hasAcbl && !hasAvg && !hasSum {
		err = fmt.Errorf("生成报表%s的LineColumnData出错，配置错误：列没有配置groupCalc属性", repConf.Name)
		return
	}
	if hasSum && (hasAcbl || hasAvg) {
		err = fmt.Errorf("生成报表%s的LineColumnData出错，配置错误：对于字段%s不能同时sum和acbl或者sum和avg计算", repConf.Name, colName)
		return
	}
	if !strings.Contains(colName, "ACBL_") {
		hasAcbl = false
		hasSum = true
		hasAvg = false
	}
	var sqlStr string
	pipe := base.RedisCli.Pipeline()
	if hasAcbl && count1 == 0 {
		sqlStr = fmt.Sprintf("select DTID,sum(%s) from %s where DTID in (?) and %s group by DTID", colName, repConf.DataSource.DFTable, where)
		rows, err := base.CHDB.Raw(sqlStr, pdates).Rows()
		if err != nil {
			return err
		}
		err = writeToRedis(rows, pipe, hkey1, timeLevel)
		if err != nil {
			return err
		}
	}
	if (hasAvg || hasSum) && count2 == 0 {
		if dRanges != nil {
			days := 1
			if hasAvg {
				days = dRanges.days
			}
			sqlStr = fmt.Sprintf("select max(DTID),sum(%s)/%d from %s where DTID >=? and DTID<=? and %s", colName, days, repConf.DataSource.DFTable, where)
			rows, err := base.CHDB.Raw(sqlStr, dRanges.sdate, dRanges.edate).Rows()
			if err != nil {
				return err
			}
			err = writeToRedis(rows, pipe, hkey2, timeLevel)
			if err != nil {
				return err
			}
		}
		for _, mr := range mRanges {
			days := 1
			if hasAvg {
				days = mr.days
			}
			sqlStr = fmt.Sprintf("with toString(max(MONT)) as mstr select toDate(concat(substring(mstr,1,4),'-',substring(mstr,5,2),'-01')), sumMerge(%s)/%d from %s where MONT>=? and MONT<=? and %s", colName, days, repConf.DataSource.MFTable, where)
			rows, err := base.CHDB.Raw(sqlStr, mr.smon, mr.emon).Rows()
			if err != nil {
				return err
			}
			err = writeToRedis(rows, pipe, hkey2, timeLevel)
			if err != nil {
				return err
			}
		}
	}
	var isChartCard bool
	if paramVals["isChartCard"] != nil {
		isChartCard = paramVals["isChartCard"].(bool)
	}
	if isChartCard {
		hkey := fmt.Sprintf("chain:%s", tName)
		count := base.RedisCli.Exists(ctx, hkey).Val()
		if count == 0 {
			curDate := date.Format("2006-01-02")
			srdate := date.AddDate(0, 0, -1).Format("2006-01-02")
			symdate := time.Date(date.Year(), date.Month(), 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1).Format("2006-01-02")
			snmdate := time.Date(date.Year(), 1, 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1).Format("2006-01-02")
			sqlStr = fmt.Sprintf("select DTID,sum(%s) from %s where DTID in (?,?,?,?) and %s group by DTID", colName, repConf.DataSource.DFTable, where)
			rows, err := base.CHDB.Raw(sqlStr, curDate, srdate, symdate, snmdate).Rows()
			if err != nil {
				return err
			}
			valMap := make(map[string]float64, 4)
			for rows.Next() {
				var dtid time.Time
				var acbl float64
				rows.Scan(&dtid, &acbl)
				valMap[dtid.Format("2006-01-02")] = acbl
			}
			curVal := valMap[curDate]
			srVal := valMap[srdate]
			symVal := valMap[symdate]
			snmVal := valMap[snmdate]
			pipe.HSet(ctx, hkey, "bsr", curVal-srVal)
			pipe.HSet(ctx, hkey, "bsy", curVal-symVal)
			pipe.HSet(ctx, hkey, "bsn", curVal-snmVal)
			pipe.Expire(ctx, hkey, 8*time.Hour)
		}
	}
	_, err = pipe.Exec(ctx)
	return
}
func createCynoPieData(repConf *Config, tName string, sdate, edate time.Time, colName, where string, days int) (err error) {
	where = strings.Replace(where, "#EMID", "EMID", 1)
	drs, mr, err := parsesDateRange(&sdate, &edate)
	if err != nil {
		return
	}
	var sqlStr string
	pipe := base.RedisCli.Pipeline()
	if mr != nil {
		dfwhere := fmt.Sprintf("MONT>=%s and MONT<=%s and %s", mr.smon, mr.emon, where)
		sqlStr = fmt.Sprintf("with dictGetString('%s.DICT_CUCY','name',toUInt64(CYNO)) AS CYNM select CYNM,toFloat64(AVVL) AVVL from (select CYNO,sumMerge(%s)/%d AVVL from %s where %s group by CYNO) t ORDER BY AVVL", base.AppConf.CHDBName, colName, days, repConf.DataSource.MFTable, dfwhere)
		rows, err := base.CHDB.Raw(sqlStr).Rows()
		if err != nil {
			return err
		}
		writePieDataToRedis(rows, pipe, tName)
	}
	for _, dr := range drs {
		mfwhere := fmt.Sprintf("DTID>='%s' and DTID<='%s' and %s", dr.sdate, dr.edate, where)
		sqlStr = fmt.Sprintf("with dictGetString('%s.DICT_CUCY','name',toUInt64(CYNO)) AS CYNM select CYNM,toFloat64(AVVL) AVVL from (select CYNO,sum(%s)/%d AVVL from %s where %s group by CYNO) t ORDER BY AVVL", base.AppConf.CHDBName, colName, days, repConf.DataSource.DFTable, mfwhere)
		rows, err := base.CHDB.Raw(sqlStr).Rows()
		if err != nil {
			return err
		}
		writePieDataToRedis(rows, pipe, tName)
	}
	_, err = pipe.Exec(ctx)
	return
}

func createTpnoPieData(repConf *Config, tName string, sdate, edate time.Time, colName, where string, days int) (err error) {
	where = strings.Replace(where, "#EMID", "EMID", 1)
	drs, mr, err := parsesDateRange(&sdate, &edate)
	if err != nil {
		return
	}
	bigTabCol := base.GetBigTabCol(colName)
	if bigTabCol == nil {
		err = fmt.Errorf("宽表字段名%s在配置中不存在", colName)
		return
	}
	if bigTabCol.AcblCol == "" {
		bigTabCol.AcblCol = "ACBL1"
	}
	if bigTabCol.DtidCol == "" {
		bigTabCol.DtidCol = "DTID"
	}
	// var itemNames, itemExps []string
	// var dfTableNames, mfTableNames []string
	// var tcolName, groupBy string
	// dtidCol := "DTID"
	// switch colName {
	// case "ACBL_DGCK":
	// 	itemNames = []string{"对公活期存款", "对公通知存款", "对公定期存款", "机构保本理财存款", "公司结构性存款", "户联盈存款", "对公活期存款", "对公定期存款", "其它存款"}
	// 	itemExps = []string{"CDK_ACBL_11", "CDK_ACBL_12", "CDK_ACBL_13", "CDK_ACBL_14", "CDK_ACBL_142", "CDK_ACBL_143", "CDK_ACBL_41", "CDK_ACBL_42", "CDK_ACBL_15"}
	// case "ACBL_DGCK_YBCK":
	// 	itemNames = []string{"对公活期存款", "对公通知存款", "对公定期存款", "其它存款"}
	// 	itemExps = []string{"CDK_ACBL_11", "CDK_ACBL_12", "CDK_ACBL_13", "CDK_ACBL_15"}
	// case "ACBL_DGDK":
	// 	itemNames = []string{"大型公司贷款", "中型公司贷款", "小微公司贷款"}
	// 	itemExps = []string{"ACBL_DGDK_DX", "ACBL_DGDK_ZX", "ACBL_DGDK_XW"}
	// case "ACBL_CX":
	// 	itemNames = []string{"活期储蓄存款", "个人通知存款", "定期储蓄存款", "个人保本理财", "个人结构性存款"}
	// 	itemExps = []string{"CDK_ACBL_51", "CDK_ACBL_52", "CDK_ACBL_53", "CDK_ACBL_54", "CDK_ACBL_55"}
	// case "ACBL_CX_YBCX":
	// 	itemNames = []string{"活期储蓄存款", "个人通知存款", "定期储蓄存款"}
	// 	itemExps = []string{"CDK_ACBL_51", "CDK_ACBL_52", "CDK_ACBL_53"}
	// case "ACBL_GRDK":
	// 	itemNames = []string{"个人住房贷款", "个人商用房贷款", "个人经营性贷款", "个人消费性贷款", "个人其它贷款"}
	// 	itemExps = []string{"ACBL_GRDK_ZF", "ACBL_GRDK_SYF", "ACBL_GRDK_JYD", "ACBL_GRDK_XFD", "ACBL_GRDK_QT"}
	// case "ACBL_ZJL":
	// 	itemNames = []string{"储蓄", "非保本理财", "基金", "国债", "三方代销"}
	// 	itemExps = []string{"ACBL_CX", "ACBL_LS_FBBLC", "ACBL_LS_JJ", "ACBL_LS_GZ", "ACBL_LS_SFDX"}
	// 	tcolName = "ACBL1"
	// case "ZS_HJ":
	// 	itemNames = []string{"公司类", "交易银行类", "金融市场类", "投行类", "零售类", "其它"}
	// 	itemExps = []string{"ZS_GS", "ZS_GJ", "ZS_TY", "ZS_TH", "ZS_LS", "ZS_QT"}
	// case "ZS_DGTX":
	// 	itemNames = []string{"公司类", "交易银行类", "金融市场类", "投行类", "其它"}
	// 	itemExps = []string{"ZS_GS", "ZS_GJ", "ZS_TY", "ZS_TH", "ZS_QT"}
	// case "ZS_LS":
	// 	itemNames = []string{"非补录", "非保本理财", "贵金属", "基金", "私行", "信用卡分期"}
	// 	itemExps = []string{"ZS_LS_FBL", "ZS_LS_4773", "ZS_LS_4771", "ZS_LS_4775", "ZS_LS_4772", "ZS_LS_4774"}
	// case "ZS_LS_BL":
	// 	itemNames = []string{"非保本理财", "贵金属", "基金", "私行", "信用卡分期"}
	// 	itemExps = []string{"ZS_LS_4773", "ZS_LS_4771", "ZS_LS_4775", "ZS_LS_4772", "ZS_LS_4774"}
	// case "ACBL_GS_FBBLC":
	// 	dfTableNames = []string{"DF_COMP_FANA_ACBL"}
	// 	mfTableNames = []string{"MF_COMP_FANA_ACBL"}
	// 	tcolName = "ACBL1"
	// 	groupBy = fmt.Sprintf("dictGetString('%s.DICT_FAPU', 'PUNM', toUInt64(PUID))", base.AppConf.CHDBName)
	// case "ACBL_LS_FBBLC":
	// 	dfTableNames = []string{"DF_INDI_FANA_ACBL"}
	// 	mfTableNames = []string{"MF_INDI_FANA_ACBL"}
	// 	tcolName = "ACBL1"
	// 	groupBy = fmt.Sprintf("dictGetString('%s.DICT_FAPU', 'PUNM', toUInt64(PUID))", base.AppConf.CHDBName)
	// case "SALE_GS_FBBLC":
	// 	dfTableNames = []string{"DF_FIN_TBTRANSCFM"}
	// 	mfTableNames = []string{"MF_FIN_TBTRANSCFM"}
	// 	tcolName = "TRMN"
	// 	groupBy = fmt.Sprintf("dictGetString('%s.DICT_FAPU', 'PUNM', toUInt64(PUID))", base.AppConf.CHDBName)
	// case "SALE_LS_FBBLC":
	// 	dfTableNames = []string{"DF_FIN_TBTRANSCFM"}
	// 	mfTableNames = []string{"MF_FIN_TBTRANSCFM"}
	// 	tcolName = "TRMN"
	// 	groupBy = fmt.Sprintf("dictGetString('%s.DICT_FAPU', 'PUNM', toUInt64(PUID))", base.AppConf.CHDBName)
	// case "SALE_LS_SFDX":
	// 	dfTableNames = []string{"mysql.SH_INDI_SHZSDR"}
	// 	tcolName = "toFloat64(JE)"
	// 	dtidCol = "JYRQ"
	// 	groupBy = "CPNM"
	// case "SALE_LS_JJ":
	// 	dfTableNames = []string{"DF_FND_TBTRANSCFM"}
	// 	mfTableNames = []string{"MF_FND_TBTRANSCFM"}
	// 	groupBy = "joinGet('JOIN_FUND_PROD', 'PUNM', PUNO)"
	// 	tcolName = "TRMN"
	// case "SALE_LS_JJDT":
	// 	dfTableNames = []string{"DF_CUST_FUND_INVEST"}
	// 	mfTableNames = []string{"MF_CUST_FUND_INVEST"}
	// 	groupBy = "joinGet('JOIN_FUND_PROD', 'PUNM', PUNO)"
	// 	tcolName = "FIX_INVEST_AMT"
	// case "SALE_LS_GZ":
	// 	dfTableNames = []string{"DF_CUST_NATION_DEBT_INVEST"}
	// 	mfTableNames = []string{"DF_CUST_NATION_DEBT_INVEST"}
	// 	tcolName = "TRMN"
	// 	groupBy = "joinGet('JOIN_BOND_TYPE', 'TPNM', BOND_TYPE_CD)"
	// case "SALE_LS_JYGJS":
	// 	dfTableNames = []string{"DF_CUST_TNM_INVEST"}
	// 	mfTableNames = []string{"MF_CUST_TNM_INVEST"}
	// 	tcolName = "AMT"
	// 	groupBy = "if(AGT_TYPE_CD='681','库存协议','持仓协议')"
	// case "SALE_LS_SWGJS":
	// 	dfTableNames = []string{"mysql.SH_INDI_GJSZSDR"}
	// 	tcolName = "toFloat64(JE)"
	// 	dtidCol = "JYRQ"
	// 	groupBy = "CPNM"
	// case "SALE_LS_BX":
	// 	dfTableNames = []string{"T_CM_CUST_INSURE_INVEST_M"}
	// 	tcolName = "GUAR_FEE"
	// 	dtidCol = "ISSUE_SINGLE_DT"
	// 	groupBy = "INSURE_TYPE_NAME"
	// case "ACBL_DGDK_BL":
	// 	dfTableNames = []string{"DF_CM_CORP_LOAN_DUBIL", "DF_CM_TRADE_FIN_LOAN_DUBIL", "DF_CM_REP_OD_DUBIL"}
	// 	mfTableNames = []string{"MF_CM_CORP_LOAN_DUBIL", "MF_CM_TRADE_FIN_LOAN_DUBIL", "MF_CM_REP_OD_DUBIL"}
	// 	tcolName = "ACBL1"
	// 	groupBy = fmt.Sprintf("dictGetString('%s.DICT_WJFL', 'name', toUInt64(FL5J-300))", base.AppConf.CHDBName)
	// case "ACBL_GRDK_BL":
	// 	dfTableNames = []string{"DF_CM_RETAIL_LOAN_DUBIL"}
	// 	mfTableNames = []string{"MF_CM_RETAIL_LOAN_DUBIL"}
	// 	tcolName = "ACBL1"
	// 	groupBy = fmt.Sprintf("dictGetString('%s.DICT_WJFL', 'name', toUInt64(FL5J-300))", base.AppConf.CHDBName)
	// case "FTP_DGCK", "FTP_DGDK":
	// 	dfTableNames = []string{"T_COMP_FACT_ASSE"}
	// 	tcolName = "MNLR"
	// 	groupBy = fmt.Sprintf("dictGetString('%s.DICT_ITEM', 'TPNM', toUInt64(ITID))", base.AppConf.CHDBName)
	// case "ZBCB_GS", "ZBGB_LS":
	// 	dfTableNames = []string{"DF_EV_ECON_CAP"}
	// 	tcolName = "ZBCB"
	// 	groupBy = fmt.Sprintf("dictGetString('%s.DICT_ITEM', 'TPNM', toUInt64(ITID))", base.AppConf.CHDBName)
	// case "FTP_CXCK":
	// 	dfTableNames = []string{"T_INDI_SAVE_FACT_ASSE", "T_INDI_FANA_FACT_ASSE"}
	// 	tcolName = "MNLR"
	// 	groupBy = fmt.Sprintf("dictGetString('%s.DICT_ITEM', 'TPNM', toUInt64(ITID))", base.AppConf.CHDBName)
	// case "FTP_GRDK":
	// 	dfTableNames = []string{"T_INDI_LOAN_FACT_ASSE"}
	// 	tcolName = "MNLR"
	// 	groupBy = "dictGetString('DICT_INDI_LAKD', 'name', toUInt64(KDID))"
	// case "ACBL_GS_WTDK", "CDK_ACBL_81", "CDK_ACBL_82", "CDK_ACBL_83":
	// 	dfTableNames = []string{"DF_COMP_ACCO_ACBL"}
	// 	mfTableNames = []string{"MF_COMP_ACCO_ACBL"}
	// 	tcolName = "ACBL1"
	// 	groupBy = fmt.Sprintf("dictGetString('%s.DICT_ITEM', 'TPNM', toUInt64(ITID))", base.AppConf.CHDBName)
	// default:
	// 	err = fmt.Errorf("生成报表%s数据出错，参数没有实现colName:%s取数逻辑", repConf.Name, colName)
	// 	return
	// }
	pipe := base.RedisCli.Pipeline()
	var sqlStr string
	if len(bigTabCol.Children) > 0 {
		itemNames := make([]string, 0, 20)
		for _, exp := range bigTabCol.Children {
			btc := base.GetBigTabCol(exp)
			if btc == nil {
				err = fmt.Errorf("宽表字段%s中的子字段%s在配置中不存在", colName, exp)
				return
			}
			itemNames = append(itemNames, btc.Desc)
		}
		if mr != nil {
			var buff bytes.Buffer
			for _, exp := range bigTabCol.Children {

				buff.WriteString("sumMerge(")
				buff.WriteString(exp)
				buff.WriteString(")/")
				buff.WriteString(strconv.Itoa(days))
				buff.WriteString(",")
			}
			buff.Truncate(buff.Len() - 1)
			mfwhere := fmt.Sprintf("MONT>=%s and MONT<=%s and %s", mr.smon, mr.emon, where)
			sqlStr = fmt.Sprintf("select %s from %s where %s", buff.String(), repConf.DataSource.MFTable, mfwhere)
			row := base.CHDB.Raw(sqlStr).Row()
			writeTpnoPieDataToRedis(row, pipe, tName, itemNames)
		}
		for _, dr := range drs {
			var buff bytes.Buffer
			for _, exp := range bigTabCol.Children {
				buff.WriteString("sum(")
				buff.WriteString(exp)
				buff.WriteString(")/")
				buff.WriteString(strconv.Itoa(days))
				buff.WriteString(",")
			}
			buff.Truncate(buff.Len() - 1)
			dfwhere := fmt.Sprintf("%s>='%s' and %s<='%s' and %s", bigTabCol.DtidCol, dr.sdate, bigTabCol.DtidCol, dr.edate, where)
			sqlStr = fmt.Sprintf("select %s from %s where %s", buff.String(), repConf.DataSource.DFTable, dfwhere)
			row := base.CHDB.Raw(sqlStr).Row()
			writeTpnoPieDataToRedis(row, pipe, tName, itemNames)
		}
	} else {
		if len(bigTabCol.MfTables) == 0 {
			dfwhere := fmt.Sprintf("%s>='%s' and %s<='%s' and %s", bigTabCol.DtidCol, sdate.Format("2006-01-02"), bigTabCol.DtidCol, edate.Format("2006-01-02"), where)
			for _, dft := range bigTabCol.DfTables {
				sqlStr = fmt.Sprintf("with %s as GBNM select GBNM,sum(%s) from %s where %s group by GBNM", bigTabCol.GroupBy, bigTabCol.AcblCol, dft, dfwhere)
				rows, err := base.CHDB.Raw(sqlStr).Rows()
				if err != nil {
					return err
				}
				writePieDataToRedis(rows, pipe, tName)
			}
		} else {
			drs, mr, err := parsesDateRange(&sdate, &edate)
			if err != nil {
				return err
			}
			days := int(edate.Sub(sdate).Hours())/24 + 1
			if mr != nil {
				for _, dmt := range bigTabCol.MfTables {
					mfwhere := fmt.Sprintf("MONT>=%s and MONT<=%s and %s", mr.smon, mr.emon, where)
					sqlStr = fmt.Sprintf("with %s as GBNM select GBNM,sumMerge(%s)/%d AVVL from %s where %s group by GBNM", bigTabCol.GroupBy, bigTabCol.AcblCol, days, dmt, mfwhere)
					rows, err := base.CHDB.Raw(sqlStr).Rows()
					if err != nil {
						return err
					}
					writePieDataToRedis(rows, pipe, tName)
				}
			}
			for _, dr := range drs {
				for _, dft := range bigTabCol.DfTables {
					dfwhere := fmt.Sprintf("%s>='%s' and %s<='%s' and %s", bigTabCol.DtidCol, dr.sdate, bigTabCol.DtidCol, dr.edate, where)
					sqlStr = fmt.Sprintf("with %s as GBNM select GBNM,sum(%s)/%d from %s where %s group by GBNM", bigTabCol.GroupBy, bigTabCol.AcblCol, days, dft, dfwhere)
					rows, err := base.CHDB.Raw(sqlStr).Rows()
					if err != nil {
						return err
					}
					writePieDataToRedis(rows, pipe, tName)
				}
			}
		}
	}
	_, err = pipe.Exec(ctx)
	return
}

//createPieData 生成Pie分析数据
func createPieData(repConf *Config, tName string, paramVals map[string]interface{}) (err error) {
	if repConf.DataSource.CacheType != "redis" {
		err = fmt.Errorf("生成报表%s的PieDate出错，DataSource.CacheType必须为redis", repConf.Name)
		return
	}
	if repConf.DataSource.TmpTableName == "" {
		err = fmt.Errorf("生成报表%s的PieDate出错，DataSource.TmpTableName不能为空", repConf.Name)
		return
	}
	var isAvg, isSum bool
	for _, col := range repConf.Columns {
		if col.GroupCalc == "avg" {
			if isAvg || isSum {
				err = fmt.Errorf("生成报表%s的PieData出错，配置错误：groupCalc为avg或者sum的列只能有一个", repConf.Name)
				return
			}
			isAvg = true
		} else if col.GroupCalc == "sum" {
			if isAvg || isSum {
				err = fmt.Errorf("生成报表%s的PieData出错，配置错误：groupCalc为avg或者sum的列只能有一个", repConf.Name)
				return
			}
			isSum = true
		}
	}
	if !isAvg && !isSum {
		err = fmt.Errorf("生成报表%s的PieData出错，配置错误：groupCalc为avg或者sum的列没有定义", repConf.Name)
		return
	}
	hkey := fmt.Sprintf("pie:%s", tName)
	reptLock.Lock(tName)
	defer reptLock.Unlock(tName)
	count := base.RedisCli.Exists(ctx, hkey).Val()
	if count > 0 {
		return
	}
	var colName string
	if _, ok := paramVals["colName"]; ok {
		colName = paramVals["colName"].(string)
	}
	if colName == "" {
		err = fmt.Errorf("生成报表%s的PieDate出错，参数colName不能为空", repConf.Name)
		return
	}
	var sdate, edate time.Time
	if _, ok := paramVals["sdate"]; ok {
		sdate = paramVals["sdate"].(time.Time)
	}
	if sdate.IsZero() {
		err = fmt.Errorf("生成报表%s的PieDate出错，参数sdate不能为空", repConf.Name)
		return
	}
	if _, ok := paramVals["edate"]; ok {
		edate = paramVals["edate"].(time.Time)
	}
	if edate.IsZero() {
		err = fmt.Errorf("生成报表%s的PieDate出错，参数edate不能为空", repConf.Name)
		return
	}
	var cyno string
	if _, ok := paramVals["cyno"]; ok {
		cyno = paramVals["cyno"].(string)
	}
	var where string
	if cyno == "1" {
		where = "1=1"
	} else if cyno == "2" {
		where = "CYNO='01'"
	} else if cyno == "3" {
		where = "CYNO<>'01'"
	} else {
		err = fmt.Errorf("生成报表%s数据出错，参数cyno的值%s无效，必须为1，2或者3", repConf.Name, cyno)
		return
	}
	var itemType string
	if _, ok := paramVals["itemType"]; ok {
		itemType = paramVals["itemType"].(string)
	}
	if itemType == "" {
		err = fmt.Errorf("生成报表%s的PieDate出错，参数itemType不能为空", repConf.Name)
		return
	}
	var emplLevel string
	if _, ok := paramVals["emplLevel"]; ok {
		emplLevel = paramVals["emplLevel"].(string)
	}
	var emplID, cuid int
	if _, ok := paramVals["emplID"]; ok {
		emplID = paramVals["emplID"].(int)
	}
	switch emplLevel {
	case "all", "all_gs", "all_ls", "all_gj":
	case "dept1":
		where = fmt.Sprintf("%s and EMID IN (SELECT toUInt32(EMID) FROM DICT_USER_DEPT WHERE PDID=%d)", where, emplID)
	case "dept2":
		where = fmt.Sprintf("%s and EMID IN (SELECT toUInt32(EMID) FROM DICT_USER_DEPT WHERE DPID=%d)", where, emplID)
	case "user":
		where = fmt.Sprintf("%s and EMID=%d", where, emplID)
	default:
		err = fmt.Errorf("生成报表%s的PieDate出错，参数emplLevel必须为all,dept1,dtpt2,user中的一项", repConf.Name)
		return
	}
	if paramVals["cuid"] != nil {
		cuid = paramVals["cuid"].(int)
	}
	if cuid > 0 {
		where = fmt.Sprintf("%s and CUID=%d", where, cuid)
	}
	bigTabCol := base.GetBigTabCol(colName)
	if bigTabCol == nil {
		err = fmt.Errorf("宽表字段名%s在配置中不存在", colName)
		return
	}
	if itemType != "CYNO" && bigTabCol.Where != "" {
		w := strings.ReplaceAll(bigTabCol.Where, "@edate", edate.Format("2006-01-02"))
		if where == "" {
			where = w
		} else {
			where = fmt.Sprintf("%s and %s", where, w)
		}
	}
	days := 1
	if isAvg {
		days = int(edate.Sub(sdate).Hours())/24 + 1
	}
	var groupBy, with, withCol string
	switch itemType {
	case "TPNO":
		return createTpnoPieData(repConf, tName, sdate, edate, colName, where, days)
	case "CYNO":
		return createCynoPieData(repConf, tName, sdate, edate, colName, where, days)
	case "ITID":
		if strings.HasPrefix(colName, "ZS_") {
			with = fmt.Sprintf("WITH dictGetString('%s.DICT_MIDO_TYPE','ITNM',toUInt64(ITID)) AS ITNM", base.AppConf.CHDBName)
			withCol = "ITNM"
			groupBy = "ITID"
		} else {
			with = fmt.Sprintf("WITH dictGetString('%s.DICT_ITEM','ITNM',toUInt64(ITID)) AS ITNM", base.AppConf.CHDBName)
			withCol = "ITNM"
			groupBy = "ITID"
		}
	case "PERD":
		if strings.HasPrefix(colName, "ZS_") {
			err = fmt.Errorf("生成报表%s的pie数据出错，字段colName:%s没有PERD的取数逻辑", repConf.Name, colName)
			return
		}
		with = fmt.Sprintf("WITH dictGetString('%s.DICT_PERD','RDNM',toUInt64(PERD)) AS RDNM", base.AppConf.CHDBName)
		withCol = "RDNM"
		groupBy = "PERD"
	}

	if bigTabCol.AcblCol == "" {
		bigTabCol.AcblCol = "ACBL1"
	}
	if bigTabCol.DtidCol == "" {
		bigTabCol.DtidCol = "DTID"
	}
	// var dfTableNames, mfTableNames []string
	// var tcolName string
	// switch colName {
	// //对公存款
	// case "ACBL_DGCK", "ACBL_DGCK_BBLC", "ACBL_DGCK_DECD", "ACBL_DGCK_BZJ", "CDK_ACBL_11", "CDK_ACBL_12", "CDK_ACBL_13", "CDK_ACBL_14", "CDK_ACBL_142", "CDK_ACBL_143", "CDK_ACBL_15", "CDK_ACBL_41", "CDK_ACBL_42", "ACBL_DGCK_YBCK":
	// 	dfTableNames = []string{"DF_CM_CORP_CUST_DPSIT", "DF_CM_CORP_CUST_BREAK_EVEN_CHREM_INVEST"}
	// 	mfTableNames = []string{"MF_CM_CORP_CUST_DPSIT", "MF_CM_CORP_CUST_BREAK_EVEN_CHREM_INVEST"}
	// 	tcolName = "ACBL1"
	// //对公贷款
	// case "ACBL_DGDK", "ACBL_DGDK_DX", "ACBL_DGDK_ZX", "ACBL_DGDK_XW", "CDK_ACBL_21", "CDK_ACBL_22", "CDK_ACBL_23", "CDK_ACBL_232", "CDK_ACBL_24", "CDK_ACBL_25", "CDK_ACBL_26", "ACBL_DGDK_YBDK":
	// 	dfTableNames = []string{"DF_CM_CORP_LOAN_DUBIL", "DF_CM_DISCNT_LOAN", "DF_CM_TRADE_FIN_LOAN_DUBIL", "DF_CM_REP_OD_DUBIL"}
	// 	mfTableNames = []string{"MF_CM_CORP_LOAN_DUBIL", "MF_CM_DISCNT_LOAN", "MF_CM_TRADE_FIN_LOAN_DUBIL", "MF_CM_REP_OD_DUBIL"}
	// 	tcolName = "ACBL1"
	// //对公普惠贷款
	// case "ACBL_DGDK_PH":
	// 	dfTableNames = []string{"DF_CM_CORP_LOAN_DUBIL"}
	// 	mfTableNames = []string{"MF_CM_CORP_LOAN_DUBIL"}
	// 	tcolName = "ACBL1"
	// //对公不良贷款
	// case "ACBL_DGDK_BL":
	// 	dfTableNames = []string{"DF_CM_CORP_LOAN_DUBIL", "DF_CM_TRADE_FIN_LOAN_DUBIL", "DF_CM_REP_OD_DUBIL"}
	// 	mfTableNames = []string{"MF_CM_CORP_LOAN_DUBIL", "MF_CM_TRADE_FIN_LOAN_DUBIL", "MF_CM_REP_OD_DUBIL"}
	// 	tcolName = "ACBL1"
	// //储蓄存款
	// case "ACBL_CX", "ACBL_CX_BBLC", "ACBL_CX_DECD", "CDK_ACBL_51", "CDK_ACBL_52", "CDK_ACBL_53", "CDK_ACBL_54", "CDK_ACBL_55", "ACBL_CX_YBCX":
	// 	dfTableNames = []string{"DF_CM_RETAIL_CUST_DPSIT", "DF_CM_RETAIL_CUST_BREAK_EVEN_CHREM_INVEST"}
	// 	mfTableNames = []string{"MF_CM_RETAIL_CUST_DPSIT", "MF_CM_RETAIL_CUST_BREAK_EVEN_CHREM_INVEST"}
	// 	tcolName = "ACBL1"
	// //个人贷款
	// case "ACBL_GRDK", "ACBL_GRDK_ZF", "ACBL_GRDK_SYF", "ACBL_GRDK_JYD", "ACBL_GRDK_XFD", "ACBL_GRDK_QT", "ACBL_GRDK_PH", "ACBL_GRDK_JED", "ACBL_GRDK_BL", "CDK_ACBL_61", "CDK_ACBL_62":
	// 	dfTableNames = []string{"DF_CM_RETAIL_LOAN_DUBIL"}
	// 	mfTableNames = []string{"MF_CM_RETAIL_LOAN_DUBIL"}
	// 	tcolName = "ACBL1"
	// case "ZS_HJ", "ZS_DGTX", "ZS_GS", "ZS_GJ", "ZS_TY", "ZS_TH", "ZS_QT", "ZS_LS", "ZS_LS_FBL", "ZS_LS_BL", "ZS_LS_4773", "ZS_LS_4771", "ZS_LS_4775", "ZS_LS_4772", "ZS_LS_4774":
	// 	dfTableNames = []string{"DF_CM_BIZ_FEE"}
	// 	mfTableNames = []string{"MF_CM_BIZ_FEE"}
	// 	tcolName = "ZS"
	// 	if itemType == "PERD" {
	// 		err = fmt.Errorf("生成报表%s的pie数据出错，字段colName:%s没有定义%s取数逻辑", repConf.Name, colName, itemType)
	// 		return
	// 	}
	// //对公非保本理财
	// case "ACBL_GS_FBBLC":
	// 	dfTableNames = []string{"DF_COMP_FANA_ACBL"}
	// 	mfTableNames = []string{"MF_COMP_FANA_ACBL"}
	// 	tcolName = "ACBL1"
	// 	if itemType == "ITID" {
	// 		err = fmt.Errorf("生成报表%s的pie数据出错，字段colName:%s没有定义%s取数逻辑", repConf.Name, colName, itemType)
	// 		return
	// 	}
	// //委托贷款,银承,保函,信用证
	// case "ACBL_GS_WTDK", "CDK_ACBL_81", "CDK_ACBL_82", "CDK_ACBL_83":
	// 	dfTableNames = []string{"DF_COMP_ACCO_ACBL"}
	// 	mfTableNames = []string{"MF_COMP_ACCO_ACBL"}
	// 	tcolName = "ACBL1"
	// //资金量
	// case "ACBL_ZJL":
	// 	dfTableNames = []string{"DF_INDI_ACCO_ACBL", "DF_INDI_FANA_ACBL", "DF_INDI_CLIE_ACBL"}
	// 	mfTableNames = []string{"MF_INDI_ACCO_ACBL", "MF_INDI_FANA_ACBL", "MF_INDI_CLIE_ACBL"}
	// 	tcolName = "ACBL1"
	// //同业
	// case "ACBL_TYZC", "CDK_ACBL_30", "CDK_ACBL_31", "CDK_ACBL_32":
	// 	dfTableNames = []string{"DF_CM_IBANK_CUST_BIZ"}
	// 	mfTableNames = []string{"MF_CM_IBANK_CUST_BIZ"}
	// 	tcolName = "ACBL1"
	// //FTP利润
	// case "FTP_DGCK", "FTP_DGDK":
	// 	dfTableNames = []string{"T_COMP_FACT_ASSE"}
	// 	tcolName = "MNLR"
	// case "FTP_CXCK":
	// 	dfTableNames = []string{"T_INDI_SAVE_FACT_ASSE", "T_INDI_FANA_FACT_ASSE"}
	// 	tcolName = "MNLR"
	// case "FTP_GRDK":
	// 	dfTableNames = []string{"T_INDI_LOAN_FACT_ASSE"}
	// 	tcolName = "MNLR"
	// //资本成本
	// case "ZBCB_GS", "ZBCB_LS":
	// 	dfTableNames = []string{"DF_EV_ECON_CAP"}
	// 	tcolName = "ZBCB"
	// default:
	// 	err = fmt.Errorf("生成报表%s的pie数据出错，字段colName:%s没有定义%s取数逻辑", repConf.Name, colName, itemType)
	// 	return
	// }
	var strSql string
	pipe := base.RedisCli.Pipeline()
	if len(bigTabCol.MfTables) == 0 {
		dfwhere := fmt.Sprintf("DTID>='%s' and DTID<='%s' and %s", sdate.Format("2006-01-02"), edate.Format("2006-01-02"), where)
		for _, dft := range bigTabCol.DfTables {
			strSql = fmt.Sprintf("%s select %s,sum(%s)/%d from %s where %s group by %s", with, withCol, bigTabCol.AcblCol, days, dft, dfwhere, groupBy)
			rows, err := base.CHDB.Raw(strSql).Rows()
			if err != nil {
				return err
			}
			writePieDataToRedis(rows, pipe, tName)
		}
	} else {
		drs, mr, err := parsesDateRange(&sdate, &edate)
		if err != nil {
			return err
		}
		if mr != nil {
			for _, dmt := range bigTabCol.MfTables {
				mfwhere := fmt.Sprintf("MONT>=%s and MONT<=%s and %s", mr.smon, mr.emon, where)
				strSql = fmt.Sprintf("%s select %s,sumMerge(%s)/%d AVVL from %s where %s group by %s", with, withCol, bigTabCol.AcblCol, days, dmt, mfwhere, groupBy)
				rows, err := base.CHDB.Raw(strSql).Rows()
				if err != nil {
					return err
				}
				writePieDataToRedis(rows, pipe, tName)
			}
		}
		for _, dr := range drs {
			for _, dft := range bigTabCol.DfTables {
				dfwhere := fmt.Sprintf("DTID>='%s' and DTID<='%s' and %s", dr.sdate, dr.edate, where)
				strSql = fmt.Sprintf("%s select %s,sum(%s)/%d AVVL from %s where %s group by %s", with, withCol, bigTabCol.AcblCol, days, dft, dfwhere, groupBy)
				rows, err := base.CHDB.Raw(strSql).Rows()
				if err != nil {
					return err
				}
				writePieDataToRedis(rows, pipe, tName)
			}
		}
	}
	_, err = pipe.Exec(ctx)
	return
}

// var whereMap = map[string]string{
// 	"ACBL_DGCK_BBLC": "ITID IN (SELECT toUInt32(ITID) FROM DICT_ITEM WHERE PPNO=14)",
// 	"ACBL_DGCK_DECD": "ITID=751",
// 	"ACBL_DGCK_BZJ":  "ITID IN (SELECT toUInt32(ITID) FROM DICT_ITEM WHERE TPNO IN (41,42))",
// 	"CDK_ACBL_11":    "ITID IN (SELECT toUInt32(ITID) FROM DICT_ITEM WHERE TPNO=11)",
// 	"CDK_ACBL_12":    "ITID IN (SELECT toUInt32(ITID) FROM DICT_ITEM WHERE TPNO=12)",
// 	"CDK_ACBL_13":    "ITID IN (SELECT toUInt32(ITID) FROM DICT_ITEM WHERE TPNO=13)",
// 	"CDK_ACBL_14":    "ITID IN (SELECT toUInt32(ITID) FROM DICT_ITEM WHERE TPNO=14)",
// 	"CDK_ACBL_142":   "ITID IN (SELECT toUInt32(ITID) FROM DICT_ITEM WHERE TPNO=142)",
// 	"CDK_ACBL_143":   "ITID IN (SELECT toUInt32(ITID) FROM DICT_ITEM WHERE TPNO=143)",
// 	"CDK_ACBL_15":    "ITID IN (SELECT toUInt32(ITID) FROM DICT_ITEM WHERE TPNO=15)",
// 	"CDK_ACBL_41":    "ITID IN (SELECT toUInt32(ITID) FROM DICT_ITEM WHERE TPNO=41)",
// 	"CDK_ACBL_42":    "ITID IN (SELECT toUInt32(ITID) FROM DICT_ITEM WHERE TPNO=42)",
// 	"ACBL_DGCK_YBCK": "ITID IN (SELECT toUInt32(ITID) FROM DICT_ITEM WHERE FPNO=1 AND PPNO<>'14' AND TPNO NOT IN (41,42))",
// 	"ACBL_GS_WTDK":   "ITID IN (SELECT toUInt32(ITID) FROM DICT_ITEM WHERE TPNO=86)",
// 	"CDK_ACBL_81":    "ITID IN (SELECT toUInt32(ITID) FROM DICT_ITEM WHERE TPNO=81)",
// 	"CDK_ACBL_82":    "ITID IN (SELECT toUInt32(ITID) FROM DICT_ITEM WHERE TPNO=82)",
// 	"CDK_ACBL_83":    "ITID IN (SELECT toUInt32(ITID) FROM DICT_ITEM WHERE TPNO=83)",
// 	"ZS_DGTX":        "ITID IN (SELECT toUInt32(ITID) FROM DICT_MIDO_TYPE WHERE TPID NOT IN (4,5)",
// 	"ZS_GS":          "ITID IN (SELECT toUInt32(ITID) FROM DICT_MIDO_TYPE WHERE TPID=1)",
// 	"ZS_GJ":          "ITID IN (SELECT toUInt32(ITID) FROM DICT_MIDO_TYPE WHERE TPID=2)",
// 	"ZS_TY":          "ITID IN (SELECT toUInt32(ITID) FROM DICT_MIDO_TYPE WHERE TPID=5)",
// 	"ZS_TH":          "ITID IN (SELECT toUInt32(ITID) FROM DICT_MIDO_TYPE WHERE TPID=3)",
// 	"ZS_QT":          "ITID IN (SELECT toUInt32(ITID) FROM DICT_MIDO_TYPE WHERE TPID IN (6,7)",
// 	"ZS_LS":          "ITID IN (SELECT toUInt32(ITID) FROM DICT_MIDO_TYPE WHERE TPID=4)",
// 	"ZS_LS_FBL":      "ITID IN (SELECT toUInt32(ITID) FROM DICT_MIDO_TYPE WHERE TPID=4 AND ITID NOT IN (4771,4772,4773,4774,4775))",
// 	"ZS_LS_BL":       "ITID IN (SELECT toUInt32(ITID) FROM DICT_MIDO_TYPE WHERE TPID=4 AND ITID IN (4771,4772,4773,4774,4775))",
// 	"ZS_LS_4773":     "ITID = 4773",
// 	"ZS_LS_4771":     "ITID = 4771",
// 	"ZS_LS_4775":     "ITID = 4775",
// 	"ZS_LS_4772":     "ITID = 4772",
// 	"ZS_LS_4774":     "ITID = 4774",
// 	"ACBL_DGDK_DX":   `CUID IN (WITH dictGetString('DICT_COMP_CLIE_QYGM_ZIPPER','QYGM',CUID,toDate('@edate')) AS QYGM SELECT CUID FROM T_COMP_CLIE WHERE QYGM IN ('大型',''))`,
// 	"ACBL_DGDK_ZX":   `CUID IN (SELECT CUID from DICT_COMP_CLIE_QYGM_ZIPPER WHERE toDate('@edate')>=SDATE AND toDate('@edate')<=EDATE AND QYGM = '中型')`,
// 	"ACBL_DGDK_XW":   `CUID IN (SELECT CUID from DICT_COMP_CLIE_QYGM_ZIPPER WHERE toDate('@edate')>=SDATE AND toDate('@edate')<=EDATE AND QYGM IN ('小型','微型'))`,
// 	"ACBL_DGDK_PH":   `JLID IN (SELECT JLID FROM DICT_COMP_PHJR_ZIPPER WHERE toDate('@edate')>=SDATE AND toDate('@edate')<=EDATE)`,
// 	"ACBL_DGDK_BL":   "FL5J IN (303,304,305)",
// 	"CDK_ACBL_21":    "ITID IN (SELECT toUInt32(ITID) FROM DICT_ITEM WHERE TPNO=21)",
// 	"CDK_ACBL_22":    "ITID IN (SELECT toUInt32(ITID) FROM DICT_ITEM WHERE TPNO=22",
// 	"CDK_ACBL_23":    "ITID IN (SELECT toUInt32(ITID) FROM DICT_ITEM WHERE TPNO=23)",
// 	"CDK_ACBL_232":   "ITID IN (SELECT toUInt32(ITID) FROM DICT_ITEM WHERE TPNO=232)",
// 	"CDK_ACBL_24":    "ITID IN (SELECT toUInt32(ITID) FROM DICT_ITEM WHERE TPNO=24)",
// 	"CDK_ACBL_25":    "ITID IN (SELECT toUInt32(ITID) FROM DICT_ITEM WHERE TPNO=25)",
// 	"CDK_ACBL_26":    "ITID IN (SELECT toUInt32(ITID) FROM DICT_ITEM WHERE TPNO=26)",
// 	"ACBL_DGDK_YBDK": "ITID IN (SELECT toUInt32(ITID) FROM DICT_ITEM WHERE FPNO=2 AND TPNO NOT IN (23,24))",
// 	"ACBL_CX_BBLC":   "ITID IN (SELECT toUInt32(ITID) FROM DICT_ITEM WHERE TPNO=54)",
// 	"ACBL_CX_DECD":   "ITID=766",
// 	"CDK_ACBL_51":    "ITID IN (SELECT toUInt32(ITID) FROM DICT_ITEM WHERE TPNO=51)",
// 	"CDK_ACBL_52":    "ITID IN (SELECT toUInt32(ITID) FROM DICT_ITEM WHERE TPNO=52)",
// 	"CDK_ACBL_53":    "ITID IN (SELECT toUInt32(ITID) FROM DICT_ITEM WHERE TPNO=53)",
// 	"CDK_ACBL_54":    "ITID IN (SELECT toUInt32(ITID) FROM DICT_ITEM WHERE TPNO=54)",
// 	"CDK_ACBL_55":    "ITID IN (SELECT toUInt32(ITID) FROM DICT_ITEM WHERE TPNO=55)",
// 	"ACBL_CX_YBCX":   "ITID IN (SELECT toUInt32(ITID) FROM DICT_ITEM WHERE TPNO NOT IN (54,55))",
// 	"ACBL_GRDK_ZF":   "KDID IN (select id from DICT_INDI_LAKD where type=1)",
// 	"ACBL_GRDK_SYF":  "KDID IN (select id from DICT_INDI_LAKD where type=5)",
// 	"ACBL_GRDK_JYD":  "KDID IN (select id from DICT_INDI_LAKD where type=2)",
// 	"ACBL_GRDK_XFD":  "KDID IN (select id from DICT_INDI_LAKD where type=4)",
// 	"ACBL_GRDK_QT":   "KDID IN (select id from DICT_INDI_LAKD where type=3)",
// 	"ACBL_GRDK_PH":   `ACID IN (SELECT JLID FROM DICT_INDI_PHJR_ZIPPER WHERE toDate('@edate')>=SDATE AND toDate('@edate')<=EDATE)`,
// 	"ACBL_GRDK_JED":  "JLID IN (SELECT JLID FROM T_CM_RETAIL_LOAN_DUBIL_SUM_M WHERE AGT_NUM LIKE 'N%')",
// 	"ACBL_GRDK_BL":   "FL5J IN (303,304,305)",
// 	"CDK_ACBL_61":    "ITID IN (SELECT toUInt32(ITID) FROM DICT_ITEM WHERE TPNO=61)",
// 	"CDK_ACBL_62":    "ITID IN (SELECT toUInt32(ITID) FROM DICT_ITEM WHERE TPNO=62)",
// 	"ACBL_TYZC":      "ITID IN (SELECT toUInt32(ITID) FROM DICT_ITEM WHERE TPNO IN (31,32))",
// 	"CDK_ACBL_30":    "ITID IN (SELECT toUInt32(ITID) FROM DICT_ITEM WHERE TPNO=30)",
// 	"CDK_ACBL_31":    "ITID IN (SELECT toUInt32(ITID) FROM DICT_ITEM WHERE TPNO=31)",
// 	"CDK_ACBL_32":    "ITID IN (SELECT toUInt32(ITID) FROM DICT_ITEM WHERE TPNO=32)",
// 	"FTP_DGCK":       "ITID IN (SELECT toUInt32(ITID) FROM DICT_ITEM WHERE FPNO=1)",
// 	"FTP_DGDK":       "ITID IN (SELECT toUInt32(ITID) FROM DICT_ITEM WHERE FPNO=2)",
// 	"ACBL_GS_FBBLC":  "PUID IN (SELECT toUInt32(PUID) FROM DICT_FAPU WHERE PUTP=3)",
// 	"ACBL_LS_FBBLC":  "PUID IN (SELECT toUInt32(PUID) FROM DICT_FAPU WHERE PUTP=3)",
// 	"SALE_GS_FBBLC":  "CUTP='0'",
// 	"SALE_LS_FBBLC":  "CUTP='1'",
// 	"SALE_LS_JJ":     "CUTP='1'",
// 	"ZBCB_GS":        "TXID<>3",
// 	"ZBCB_LS":        "TXID=3",
// 	"FTP_CXCK":       "ITID IN (SELECT toUInt32(ITID) FROM DICT_ITEM WHERE FPNO=3)",
// 	"FTP_GRDK":       "ITID IN (SELECT toUInt32(ITID) FROM DICT_ITEM WHERE FPNO=4)",
// }
